<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/4/1
 * Time: 15:53
 */

namespace SDXApi\Service;


use SDXApi\Abstracts\TemplateAbstract;
use SDXApi\Model\SeminarTemplatePageGoodsModel;
use SDXApi\Repositories\GoodsRepository;

/**
 * Class SeminarGoodsService
 * @package SDXApi\Service
 */
class SeminarGoodsService extends TemplateAbstract
{
	/**
	 * @var SeminarTemplatePageGoodsModel
	 */
	protected $seminarGoodsModel;

	/**
	 * @var GoodsRepository
	 */
	protected $goodsRepository;

	/**
	 * SeminarGoodsService constructor.
	 */
	public function __construct()
	{
		$this->seminarGoodsModel = new SeminarTemplatePageGoodsModel();
		$this->goodsRepository = new GoodsRepository();
	}

	/**
	 * Created by Jerry
	 * @param array $template
	 * @param int $userId
	 * @param int $wid
	 * @return array|bool
	 */
	public function handle($template = array(), $userId = 0, $wid = 8)
	{
		return $this->goods($template, $userId, $wid);
	}

	/**
	 * Created by Jerry
	 * @param array $page
	 * @param int $userId
	 * @param int $wid
	 * @return array|bool
	 */
	public function goods($page = array(), $userId = 0, $wid = 8)
	{

		$data = $this->getGoodsCache($page, $userId, $wid);

		foreach ($data as $key => $value) {
			$data[$key]['id'] = $value['goods_id'];
		}

		$data = array_merge($data);

		if (count($data) > 1) {
			$data = self::checkGoodsDataSort($data);
		}

		if (empty($data)) {
			return false;
		}


		$page['goods_list'] = $data;
		unset($page['font_type'], $page['font_size'], $page['font_content'], $page['font_link'], $page['image_url'], $page['image_link'], $page['image_size'], $page['template_id'], $page['goods_id']);

		return $page;
	}

	/**
	 * 获取商品缓存数据
	 * Created by Jerry
	 * @param array $page
	 * @param int $userId
	 * @param int $wid
	 * @return array
	 */
	protected function getGoodsCache($page = array(), $userId = 0, $wid = 8)
	{
		$userRank = $this->getUserRank($userId);
		$cacheName = 'seminar_template_page_goods_' . $page['id'] . '_rank_' . $userRank . '_wid_' . $wid;
		$goodsData = $this->getCache($cacheName);

		$this->goodsRepository->userId = $userId;
		$this->goodsRepository->wid = $wid;

		if (empty($goodsData)) {
			$goods = $this->findSeminarGoodsByPageId($page['id'], $wid);
			$goodsData = $this->goodsRepository->formatGoodsTransform($goods);

			if (! empty($goodsData)) {
				$this->cacheData($cacheName, $goodsData, mt_rand(3600, 4200));
			}
		}

		return $this->goodsRepository->checkGoodsPractical($goodsData);
	}

	/**
	 * 获取专题商品
	 * Created by Jerry
	 * @param int $page_id
	 * @param int $wid
	 * @return bool|mixed
	 */
	protected function findSeminarGoodsByPageId($page_id = 0, $wid = 8)
	{
		if (empty($page_id)) {
			return false;
		}

		return $this->seminarGoodsModel->findSeminarTemplateGoodsByPageId($page_id, $wid);
	}

	/**
	 * 排序
	 * Created by Jerry
	 * @param array $data
	 * @return array
	 */
	static protected function checkGoodsDataSort($data = array())
	{
		$sort = array();
		foreach ($data as $key => $value) {
			if (empty($value['goods_number'])) {
				$sort[] = $value['goods_number'];
			}
			else{
				$sort[] = $value['sort'] + 1;
			}
		}

		array_multisort($sort, SORT_NUMERIC, SORT_DESC, $data);

		return $data;
	}
}